// pages/calendar/calendar.js
//var sevenDay=[5,0,0,4,3,6,2];
var weekend="不上班";
var departmentId;
var util=require("../../utils/util.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    week:[
      '日','一','二','三','四','五','六'
    ],
    month:3,
    empty:[
      1,2,3
    ],
    allDay:[
      

    ],
      logo:[
        {
          text:"约满",
          color:'#00A8FD'
        },
        {
          text:"微信有号",
          color:'green'
        }
      ],
  },
  toExpert:function(e){
    var index=e.currentTarget.id;
   var datas=this.data.allDay;
   var day = Number(datas[index].day);
  
   

   if('condition' in datas[index] && datas[index].condition=='可约'){
     if (day == Number(index) + 1) {
       var month = this.data.month;
       if (month < 10) month = '0' + month;
       else month = month.toString();
       if(day<10)day='0'+day;
       else day=day.toString();
       var year = new Date().getFullYear();
       var chooseDate = year.toString() + '-' + month + '-' + day.toString();
       console.log(chooseDate)
     }
     else {
        var month=Number(this.data.month)+1;
        if(month<10) month='0'+month;
        else month=month.toString();
        if (day < 10) day = '0' + day;
        else day = day.toString();
        var year=new Date().getFullYear();
        var chooseDate=year.toString()+'-'+month+'-'+day.toString();
        console.log(chooseDate)
     }
     getApp().globalData.chooseDate=chooseDate;
    wx.navigateTo({
       url: '../expertList/expertList?chooseDate='+chooseDate+"&&departmentId="+departmentId,
     })
   }
   else{
     console.log('约满或者日期选择不对');
   }
  },
  loadAllDate:function(sevenDay){
    
    var time=util.formatTime(new Date);
    var month=new Date().getMonth()+1;
    var day=new Date().getDay();
   // day=day%7;
    var date=new Date().getDate();
    var firstDay=Math.abs(day-(date-1)%7);
    var allDay=[];
    var empty=[];
    var dayNum;
    switch(month){
      case 1:dayNum=31;break;
      case 3: dayNum = 31; break;
      case 5: dayNum = 31; break;
      case 7: dayNum = 31; break;
      case 8: dayNum = 31; break;
      case 10: dayNum = 31; break;
      case 12: dayNum = 31; break;
      case 4: dayNum = 30; break;
      case 6: dayNum = 30; break;
      case 9: dayNum = 30; break;
      case 10: dayNum = 30; break;
      case 2:
      {
        if(new Date().getFullYear()%4==0) dayNum=28;
        else dayNum=29;
      }
      break;
    }
    for (var i = 1; i <= dayNum; i++) {
      var one = { day: "", dayCss: "notFull" };
      if (i < 10) one.day = "0" + i;
      else one.day = i.toString();
      allDay.push(one);
    }
    var weekDay = (Number(day) + 1) > 7 ? 1 : day+1;
    
    for(var flag=0,nowDate=date+1;  flag<sevenDay.length&&nowDate<=dayNum   ;flag++,nowDate++,weekDay++){
     weekDay=weekDay>7?1:weekDay;
     
        if(weekend=="不上班"){

            if(weekDay!=6&&weekDay!=7){

                if(sevenDay[flag]>0){
                  allDay[nowDate-1].dayCss = "canChoose";
                  allDay[nowDate-1].condition = "可约";
                }
                else{
                  allDay[nowDate-1].dayCss="full";
                  allDay[nowDate-1].condition="约满";
                }

            }
            
        }
        else{
          if (sevenDay[flag] > 0) {
            allDay[nowDate-1].dayCss = "canChoose";
            allDay[nowDate-1].condition = "可约";
          }
          else {
            allDay[nowDate-1].dayCss = "full";
            allDay[nowDate-1].condition = "约满";
          }
        }
    }
    for(var i=0;i<firstDay;i++)
    empty.push(i);
    allDay[date-1].dayCss="today";
    allDay[date-1].condition="今天";
    if (nowDate > dayNum) {
     
      var nextMonth=month+1;
      nowDate%=dayNum;
      var nextMonthDay;
        
     for(var i=flag;i<sevenDay.length;i++,nowDate++,weekDay++){
       weekDay = weekDay > 7 ? 1 : weekDay;
          nextMonthDay=nowDate;
          if(nextMonthDay<10)  nextMonthDay='0'+nextMonthDay;
          else nextMonthDay=nextMonthDay.toString();
          var one = { day: "", dayCss: "notFull" };
          one.day=nextMonthDay;
          if(weekend=='不上班'){
            if(weekDay!=6&&weekDay!=7){
              if(sevenDay[i]>0) {one.dayCss="canChoose";one.condition="可约"}
              else {one.dayCss="full";one.condition="约满"};
            }
          }
          else{
            if (sevenDay[i] > 0) { one.dayCss = "canChoose"; one.condition = "可约" }
            else { one.dayCss = "full"; one.condition = "约满" };
          }
          allDay.push(one);


     }
    }
    
    this.setData(
      {
        allDay: allDay,
        empty:empty,
        month:month
      }
    );
    
  },
  requestData:function(e,o){
    console.log(e,o);
    var that=this;
    wx.request({
      url: 'https://www.liuxuanlong.xyz/fenda/wx/findGuahaoNum',
      data:{
        hospitalId:e,
        hospitalDepartmentId:o
      },
      success:function(res){
        console.log(res.data);
        if(res.data.weekendWorkStatus=='on') weekend="上班";
        else weekend="不上班";
        that.loadAllDate(res.data.guahaoNum);
      },
    })
  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    departmentId=options.departmentId;
   this.requestData(options.hospitalId,options.departmentId);
   
  console.log(options)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})